package com.molyfun.parents.activity.course;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.ask.view.FlowTagLayout;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.activity.course.coursehistory.CourseSearchHistoryManager;
import com.molyfun.parents.view.common.MfEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * 搜索家园标签列表页,点击后返回给发布页
 * 通过startActivityForResult调用
 */
public class CourseSearchActivity extends BaseActivity implements View.OnClickListener{


    public static final String FLAG_SEARCH_TAG = "search_tag";

    @InjectView(R.id.search_btn)
    ImageView mSearchBtn;
    @InjectView(R.id.search_edt)
    MfEditText mSearchEdt;
    @InjectView(R.id.base_title_left)
    ImageView mBack;
    @InjectView(R.id.history_del)
    ImageView mHistoryDel;
    @InjectView(R.id.flow_tag_layout)
    FlowTagLayout mFlowTagLayout;
    @InjectView(R.id.search)
    TextView mSearch;

    List<String> tags;

    public static final void startActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, CourseSearchActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_search);
        getSupportActionBar().setTitle("");
        ButterKnife.inject(this);

        mSearchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchTopic();
            }
        });
        mSearchEdt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    searchTopic();
                }
                return false;
            }
        });
        mBack.setOnClickListener(this);
        mHistoryDel.setOnClickListener(this);
        tags = CourseSearchHistoryManager.getSearchHistory(this);
        if(tags == null){
            tags = new ArrayList<>();
        }
        mFlowTagLayout.initData(tags);
        mFlowTagLayout.setOnTagSelectListener(new FlowTagLayout.OnTagSelectListener() {
            @Override
            public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList, int position, boolean selectedState) {
                SearchResultActivity.startActivity(CourseSearchActivity.this, tags.get(position));
            }
        });
        mSearch.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
            case R.id.search:
                searchTopic();
                break;
            case R.id.history_del:
                tags.clear();
                mFlowTagLayout.clearDataList();
                break;
        }
    }

    private void searchTopic(){
        String str = mSearchEdt.getText().toString().trim();
        SearchResultActivity.startActivity(CourseSearchActivity.this,str);
        addSearchStr(str);
    }

    private void addSearchStr(String str){
        if(!tags.contains(str)){
            tags.add(str);
            mFlowTagLayout.initData(tags);
        }
    }

    @Override
    protected void onDestroy() {
        CourseSearchHistoryManager.setSearchHistory(this, tags);
        super.onDestroy();
    }

//    @Override
//    public void onSearchTagItemClick(HomelandFeaturedResponse.HotTopic hotTopic) {
//        Intent intent = new Intent();
//        intent.putExtra(FLAG_SEARCH_TAG, hotTopic.getName());
//        setResult(Activity.RESULT_OK, intent);
//        finish();
//    }


}
